package com.ruoyi.business.config;

import Ice.Communicator;
import Ice.InitializationData;
import com.ruoyi.business.PortServerIce.QueryServantPrx;
import com.ruoyi.business.PortServerIce.TradeServantPrx;
import com.ruoyi.business.enumType.ProxyServiceType;
import com.ruoyi.business.helper.QueryServantPrxHelper;
import com.ruoyi.business.helper.TradeServantPrxHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Instant;

@Configuration
public class ProxyBeanConfig {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(ProxyBeanConfig.class);


    /**
     * 是否开启swagger
     */
    @Value("${ice.ip}")
    private String ip;

    @Value("${ice.port}")
    private String port;


    @Autowired
    public Communicator communicator;


    @Bean
    public TradeServantPrx createTradeServantPrx() {

        try {
            String con = ProxyServiceType.TradeService + ":tcp" + " -h " + ip + " -p " + port;
//            String con = "TradeService:default -p 60010";

            logger.info("con,{}",con);
            TradeServantPrx tradeServantPrx = TradeServantPrxHelper.checkedCast(communicator.stringToProxy(con));
            if (tradeServantPrx == null) {
                logger.error("tradeServantPrx 获取失败");
                System.err.println("Invalid proxy");
            }
            logger.info("tradeServantPrx  代理对象获取成功");
            return tradeServantPrx;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public QueryServantPrx createQueryServantPrx() {
        try {
            String con = ProxyServiceType.QueryService + ":tcp" + " -h " + ip + " -p " + port;
//            String con = "TradeService:default -p 60010";

            logger.info("con,{}",con);
            QueryServantPrx queryServantPrx = QueryServantPrxHelper.checkedCast(communicator.stringToProxy(con));

            if (queryServantPrx == null) {
                logger.error("QueryServantPrx 获取失败");
                System.err.println("Invalid proxy");
            }
            logger.info("QueryServantPrx  代理对象获取成功");
            return queryServantPrx;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
